/**
 * https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-ii/submissions/543936703/
 * 122.买股票的最佳时机||
 * medium 2024.07.04
 * dp
 */

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int leng=prices.size();
        int sum=0;
        for(int i=0;i<leng;++i){
            while(i<leng-1&&prices[i]>prices[i+1])
            i++;
            if(i<leng-1){
                int j=i+1;
                while(j<leng-1&&prices[j]<prices[j+1])
                    j++;
                sum+=prices[j]-prices[i];
                i=j;
            }
        }
        return sum;
    }
};